package com.example.demo.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.domain.Result;
import com.example.demo.exception.GirlException;
import com.example.demo.utils.ResultUtil;

/**
 * 对异常进行处理，返回格式化语句
 * **/
@ControllerAdvice
public class ExceptionHandle {
	
	private final static Logger log = LoggerFactory.getLogger(ExceptionHandle.class);
	
	
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result handle(Exception e){
		if(e instanceof GirlException){
			GirlException girlException = (GirlException) e;
			return ResultUtil.error(girlException.getCode(), girlException.getMessage());
		}else{
			log.error("系统异常 - {}", e);
			return ResultUtil.error(-1, "系统异常");
		}
	}
	

}
